<?php
namespace Home\Controller;
use Common\Controller\HomeController;

class OmController extends HomeController {

	//0  IN（打入）/ 1OU（打出）/ 2 FI（呼叫转移入）/ 3FW（呼叫转移出）/ 4LO（内部通话）/ 5CB（双向外呼）
	protected $m_calltypes = array('IN'=>0,'OU'=>1,'FI'=>2,'FW'=>3,'LO'=>4,'CB'=>5);
	/**
	 * OM适配器 cdr信息上报,post请求
	 *
	 * "m_id":"120160718164633-0","m_callid":49155,"m_visitor":null,"m_outer":3,"m_type":"OU",
	 * "m_tmstart":"20160718164617","m_route":"IP","m_cpn":"204","m_cdpn":"13332904710",
	 * "m_tmend":"20160718164633","m_duration":"6","m_trunknumber":"59821969",
	 * "m_recording":null,"m_extid":"204"
	 */
	public function cdr(){
		$res = array('rcode'=>0,'data'=>null,'msg'=>'');
		if( !isset( $_POST['cdr'] ) || !$_POST['cdr']){
			$res['msg'] = "上传参数错误";
		}
		else{
			$omid = isset( $_POST['omID']) ? intval( $_POST['omID']) : 0;    //omid：OM服务器ID
			$cdr = isset( $_POST['cdr']) ? (array)$_POST['cdr'] : false;
			if( $omid && $cdr ){
				$calldata = array();     				//时长统计表数据
				$customer_contact_data = array();		//客户沟通记录表数据
				$customer_data = array();				//客户表数据
				$MCallog = M("calllog");
				// $MCustomer_contact_log = M("customer_contact_log");
				// $MCustomer = M("customer");
				$calldata['call_id'] = $cdr['m_id'];
				$calldata['om_id'] = $omid;
				$map1 = array();
				$map1['omid'] = array('eq', $omid);
				$OMs = M('om_server')->where( $map1 )->find();
				$map1['subphone'] =  array('eq', $cdr['m_extid'] );
				$admin_no = M('om_subphone')->where( $map1 )->getField("admin_no");    //获取分机对应的员工用户id
				$calldata['admin_no'] = isset( $admin_no ) ? $admin_no : -1;
				$admin_sector = null;

				$map2 = array();
				$map2['user_id'] = array('eq',$admin_no);
				if( $admin_no ){
					$admin_sector = M('user')->where( $map2)->getField("sector_id");
					$admin_role = M('user')->where( $map2)->getField("role_id");
				}
				$calldata['sector_id'] = $admin_sector ? $admin_sector : -1;
				$calldata['calldate'] = date('Ymd');
				$calldata['ext_id'] = $cdr['m_extid'];
				$calldata['channel'] = '';
				$calldata['route'] = $cdr['m_route'];
				$calldata['trunkNumber'] = $cdr['m_trunknumber'];
				$calldata['area'] = isset( $cdr['area'] ) ? $cdr['area'] :null;
				$calldata['type'] = isset( $this->m_calltypes[$cdr['m_type']] ) ? $this->m_calltypes[$cdr['m_type']] : -1;
				$telephone = ( $cdr['m_cpn']  == $cdr['m_extid'] ) ? $cdr['m_cdpn'] : $cdr['m_cpn'];
				$calldata['telephone'] = $telephone;
				$begintime = date('Y-m-d H:i:s', strtotime($cdr['m_tmstart']));;
				$calldata['begintime'] = $begintime;
				$calldata['endtime'] = date('Y-m-d H:i:s', strtotime($cdr['m_tmend']));
				$calldata['calltime'] = intval( $cdr['m_duration'] );
				$calldata['mp3file'] = $cdr['m_recording'] ? $OMs['mp3_dir'].'/'.$cdr['m_recording'] : null;
				if(strlen($telephone)>6 && substr($begintime,0,4) != '1970')    //过滤掉时间为1970、电话号码小于6位的记录
					$addCalllog = $MCallog->data( $calldata )->add();     		//存入时长统计表
				if($addCalllog){
					$res['rcode'] = 1;
					$res['msg'] = 'OK';
					$res['data_Calllog'] = $addCalllog;
				}else{
					$res['msg'] = "存入数据失败";
				}
			}
			else
				$res['msg'] = "上传参数不完整";
		}
		echo json_encode( $res );
		exit();
	}

	/**
	 *  查询配置OM设备信息
	 */
	public function getOms(){
		$omlist = M('om_server')->order("omid asc")->select();
		$res = array('rcode'=>0,'msg'=>'no data','data'=>NULL);
		if( $omlist ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $omlist ;
		}else{
			$res['msg'] = "获取信息失败";
		}
		echo json_encode( $res );
		exit();
	}

	/**
	 * 查询OM对应的OM设备信息
	 */
	public function getOmSubs(){
		$omID = isset( $_GET['omID']) ? intval( $_GET['omID'] ) : '';
		$res = array('rcode'=>0,'msg'=>'no data','data'=>NULL);
		if( $omID ){
			$wherem = array();
			$wherem['omid'] = array('eq', $omID);
			$subs = M("om_subphone")->order("subphone asc")->select();
			if( $subs ){
				$res['rcode'] = 1;
				$res['msg'] = "OK";
				$res['data'] = $subs;
			}
			else
				$res['msg'] = "获取OM对应设备分机失败";
		}

		echo json_encode( $res );
		exit();
	}

}
